/*
 * Javlov - a Java toolkit for reinforcement learning with multi-agent support.
 * 
 * Copyright (c) 2009 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.javlov.world;

import java.io.Serializable;
import java.util.Observable;


/**
 * A Property represents a property, identified by name, that has a certain
 * value, and presents methods for updating and reading that value.
 * 
 * If changed, the Property notifies its observers on conditions checked
 * in method updated(); by default, it always notifies its observers of any change.
 * 
 * @author Matthijs Snel
 * @version 0.1, 02/06/2007
 * @version 0.2, 07/05/2009
 */
public class Property extends Observable implements Cloneable, Serializable {
	
	private static final long serialVersionUID = -1877274330549507100L;

	/**
	 * The property name
	 */
	private String name;
	
	/**
	 * The property's last increment.
	 */
	private double lastincr;
	
	/**
	 * The property's current value.
	 */
	private double val;
	
	/**
	 * The initial value.
	 */
	private double initVal;
	
	/**
	 * The property's maximum value. For unbounded properties this is set to
	 * positive infinity.
	 */
	private double max;
	
	/**
	 * The property's minimum value. For unbounded properties this is set to
	 * negative infinity.
	 */
	private double min;
	
	/**
	 * Creates an unbounded property with initial value 1.0.
	 * 
	 * @param s the property name.
	 */
	public Property( String name ) {
		this(name, 1.0);
	}
	
	/**
	 * Creates an unbounded property with initial value v.
	 * 
	 * @param s the property name.
	 * @param v the property's initial value.
	 */
	public Property( String name, double initialValue ) {
		this(name, initialValue, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY);
	}
	
	/**
	 * Creates a bounded property with upper bound maxval and lower bound minval.
	 * If a property should be bounded on only one side, set the unbounded side to
	 * positive/negative infinity (using the infinity fields of class Double).
	 * 
	 * @param s the property name.
	 * @param v the property's initial value.
	 * @param maxval the property's maximum value.
	 * @param minval the property's minimum value.
	 * @see Double
	 */
	public Property( String name, double initialValue, double maxval, double minval ) {
		this.name = name;
		initVal = initialValue;
		max = maxval;
		min = minval;
		reset();
	}
	
	@Override
	public boolean equals(Object o) {
		if ( o instanceof Property )
			return name.equals( ((Property) o).getName() );
		return false;
	}
	
	public double getLastIncrement() {
		return lastincr;
	}
	
	public double getMaximum() {
		return max;
	}
	
	public double getMinimum() {
		return min;
	}
	
	public String getName() {
		return name;
	}
	
	/**
	 * 
	 * @return the property's current value.
	 */
	public double getValue() {
		return val;
	}
	
	@Override
	public int hashCode() {
		return name.hashCode();
	}
	
	public void reset() {
		val = initVal;
		lastincr = 0;
	}
	
	/**
	 * Sets the property value to v if it is within the property bounds (including
	 * the bounds). It then calls method updated() to see if the property's
	 * observers should be notified of the change.
	 * 
	 * @param v the value to which the property should be set.
	 */
	public void setValue( double v ) {
		if ( v <= max && v >= min) {
			lastincr = v - val;
			val = v;
			updated();
		}
	}
	
	/**
	 * Increments (or decrements if delta is negative) the property's value 
	 * by delta. If this exceeds the property's bounds, the value is set to be
	 * exactly at the bound that is exceeded.
	 * 
	 * @param delta
	 * @see setValue(double)
	 */
	public void update( double delta ) {
		double 	r = val + delta,
				v = (r > max ? max : (r < min ? min : r));		
		setValue(v);
	}
	
	public Property clone() {
		Property p = null;
		try {
			p = (Property) super.clone();
		} catch (CloneNotSupportedException e) {
			// This is stupid, we'll never get here
			e.printStackTrace();
		}
		return p;
	}
	
	public String toString() {
		return name + ": " + val;
	}
	
	/**
	 * Notifies the property's observers if the property's value is at one of
	 * the property's bounds.
	 */
	protected void updated() {
		setChanged();
		notifyObservers();
	}
}
